<?php
if(!defined('IN_DISCUZ')) {
        exit('Access Denied');
}

require_once libfile('function/renren');

if(submitcheck('renren_bind')) {
	global $_G;
	$rr_uid = rr_get_cookie('user');
	$dz_uid = $_G['uid'];
	$tag1 = DB::result_first("SELECT tag FROM ".DB::table('renren_connect')." WHERE dz_uid='$dz_uid' ORDER BY tag DESC");
	$tag2 = DB::result_first("SELECT tag FROM ".DB::table('renren_connect')." WHERE rr_uid='$rr_uid' ORDER BY tag DESC");

	if(intval($tag1)>0 || intval($tag2)>0) {
		showmessage('绑定失败', 'home.php?mod=spacecp&ac=plugin&id=renren:spacecp');
	}
	
	rr_make_bind();
	showmessage('绑定成功', 'index.php');
}

if(submitcheck('renren_unbind')) {
	rr_unbind();
	showmessage('解除绑定成功', 'home.php?mod=spacecp&ac=plugin&id=renren:spacecp');
}

if(submitcheck('discuz_bind')) {
	$username = getgpc('username');
	$password = getgpc('password');
	
	if(!function_exists('uc_user_login')) {
		loaducenter();
	}
	$questionid = '';
	$answer = '';
	if(preg_match('/^[1-9]\d*$/', $username)) {
		$result = uc_user_login($username, $password, 1, 1, $questionid, $answer);
	} elseif(isemail($username)) {
		$result= uc_user_login($username, $password, 2, 1, $questionid, $answer);
	} else {
		$result = uc_user_login($username, $password, 0, 1, $questionid, $answer);
	}
	if(!empty($result)) {
		$uid = $result[0];
		if($uid>0) {
			$rr_uid = rr_get_bind_rr_uid($uid);
			if($rr_uid) {
				showmessage('绑定失败', 'home.php?mod=spacecp&ac=plugin&id=renren:spacecp');
			}
			rr_make_bind($uid);
			showmessage('绑定成功', 'index.php');
		}showmessage('绑定失败', 'home.php?mod=spacecp&ac=plugin&id=renren:spacecp');
	}
}
$actives = array('renren' =>' class="a"');
$isbind = rr_is_bind();
if($isbind) {
	$dz_name = $_G['member']['username'];
	require_once libfile('class/renren_api');
	$api = & renren_api::instance();
	$dz_uid = $_G['uid'];
	$rr_uid = DB::result_first("SELECT rr_uid FROM ".DB::table('renren_connect')." WHERE dz_uid='$dz_uid' ORDER BY tag DESC");
	$user_info = $api->get_user_info_without_session($rr_uid);
	$rr_name = $user_info->name;
}

if($_G['renren']) {
	require_once libfile('class/renren_api');
	$api = & renren_api::instance();
	$user_info = $api->get_user_info();
	$avatar = array('tinyurl'=>'', 
					'headurl'=>'', 
					'mainurl'=>'',
					'tinyurl'=>'',
					'headurl'=>'',
					'mainurl'=>'',
					);
	foreach(array_keys($avatar) as $k) {
		$avatar[$k] = $user_info->$k;
	}
	
	$headurl = $avatar['mainurl'];
	$uid = $_G['uid'];
	
	if(submitcheck('renren_avatar_submit')) {
		require_once libfile('function/renren');
		upload_avatar($uid, $avatar['tinyurl'], 'small');
		upload_avatar($uid, $avatar['headurl'], 'middle');
		upload_avatar($uid, $avatar['mainurl'], 'big');
	}
}

?>